//
//  UITextFeild+Extension.swift
//  Library_y
//
//  Created by JC on 2023/6/6.
//

import Foundation
import UIKit

extension UITextField {
    /// 设置不同颜色
    public func setColor(_ color: UIColor, range: NSRange) {
        guard let text = text else { return }
        let attributedString = NSMutableAttributedString(string: text)
        attributedString.addAttribute(.foregroundColor, value: color, range: range)
        self.attributedText = attributedString
    }
}

extension UITextField {
    
    /// 设置内容左边的空隙
    ///
    /// - Parameter space: 空隙
    public func leftSpace(space: CGFloat) {
        let spaceView = UIView()
        spaceView.frame = CGRect(x: 0, y: 0, width: space, height: 10)
        self.leftViewMode = .always
        self.leftView = spaceView
    }
}

extension UITextField {
    
    /// 设置内容左边的空隙
    ///
    /// - Parameter space: 空隙
    public func leftSpace(space: CGFloat, image: UIImage?) {
        let imageView = UIImageView(image: image)
        if let size = image?.size {
            imageView.frame = CGRect(x: 0, y: 0, width: size.width + space, height: size.height)
            imageView.contentMode = .center
        }
        leftViewMode = .always
        leftView = imageView
    }
    
    /// 根据左侧和右侧图像构建 UITextField
    ///
    /// - parameter frame:   矩形大小
    /// - parameter left:    左侧图像
    /// - parameter padding: 左侧图像与内容偏移大小
    /// - parameter right:   右侧图像
    ///
    /// - returns: UITextField
    public convenience init(frame: CGRect, left: UIImage?, leftPadding padding: CGFloat = 10, right: UIImage?) {
        self.init(frame: frame)
        
        let imageView = UIImageView(image: left)
        if let size = left?.size {
            imageView.frame = CGRect(x: 0, y: 0, width: size.width + padding, height: size.height)
            imageView.contentMode = .center
        }
        
        let button = UIButton()
        button.setImage(right, for: .normal)
        
        leftViewMode = .always
        leftView = imageView
        rightViewMode = .always
        rightView = button
    }
    
    /// 根据左侧和右侧图像构建 UITextField
    ///
    /// - parameter frame:         矩形大小
    /// - parameter leftImageView: 左侧图像视图
    /// - parameter right:         右侧图像
    ///
    /// - returns: UITextField
    public convenience init(frame: CGRect, leftImageView: UIImageView, right: UIImage?) {
        self.init(frame: frame)
        
        leftImageView.contentMode = .center
        
        let button = UIButton()
        button.setImage(right, for: .normal)
        
        leftViewMode = .always
        leftView = leftImageView
        rightViewMode = .always
        rightView = button
    }
    
    /// 左边图片视图
    public var imageView: UIImageView? {
        return leftView as? UIImageView
    }
    
    /// 右边点击视图
    public var button: UIButton? {
        return rightView as? UIButton
    }
}

public extension UITextField {
    
    /// 设置 placeholder
    ///
    /// - parameter placeholder: placeholder
    /// - parameter font:        placeholder 的字体
    /// - parameter color:       placeholder 的颜色
    func setPlaceholder(placeholder: String, withFont font: UIFont, andColor color: UIColor) {
        
        let attributes = [
            NSAttributedString.Key.foregroundColor: color,
            NSAttributedString.Key.font: font
        ]
        self.attributedPlaceholder = NSAttributedString(string: placeholder, attributes:attributes)
    }
    
    /// 设置 placeholder
    ///
    /// - parameter placeholder: placeholder
    /// - parameter font:        placeholder 的字体
    func setPlaceholder(placeholder: String, withFont font: UIFont) {
        
        let attributes = [
            NSAttributedString.Key.font: font
        ]
        self.attributedPlaceholder = NSAttributedString(string: placeholder, attributes:attributes)
    }
    
    /// 设置 placeholder
    ///
    /// - parameter placeholder: placeholder
    /// - parameter color:       placeholder 的颜色
    func setPlaceholder(placeholder: String, withColor color: UIColor) {
        
        let attributes = [
            NSAttributedString.Key.foregroundColor: color
        ]
        self.attributedPlaceholder = NSAttributedString(string: placeholder, attributes:attributes)
    }
}
